* This dofile creates the function to estimate bounds on effects conditional on running.

// Lee bounds for unobserved counterfactual 
cap program drop leebounds_next
program define leebounds_next, eclass
syntax, outcome_next(string) running_level_next(string) band(real) [min_unobs_term(real 0)] [max_unobs_term(real 0)] [manual_unobs_term(real 0)] [uncond_value(real 0)]
set graphics off

gen temp=`outcome_next'!=.
byso year election_type: egen temp2=max(temp)
drop if temp2==0 //function runs on the restricted sample of elections and years for which the outcome is defined for at least some candidates
drop temp*

tempname coef_outcome coeff_run R1 E1

// Impact on outcome unconditionally 
replace `outcome_next'=`uncond_value' if `running_level_next'==0 & `outcome_next'==.
rdrobust `outcome_next' running, h(`band') vce(nncluster code_district)
scalar `coef_outcome'=e(tau_cl)

// Impact on run + probability that a close winner runs 
rdrobust `running_level_next' running, h(`band') vce(nncluster code_district)
scalar `coeff_run'=e(tau_cl)
scalar `R1'=e(tau_cl_r)

// Outcome value of close winner given that she runs
rdrobust `outcome_next' running if `running_level_next'==1, h(`band') vce(nncluster code_district)
scalar `E1'=e(tau_cl_r)


// Compute 10th, 25th, 50th,75th and 90th percentiles of the outcome variable at the "threshold" for those that won in the previous election conditional on running again
preserve
keep if `running_level_next'==1
drop if missing(`outcome_next') //rdplot does not work well with missing values
qui: capture rdplot `outcome_next' running if `running_level_next'==1, nbins(20 20) genvars 
keep if rdplot_id==1  //1 is the bin id of the first bin on the rhs of the cutoff
_pctile `outcome_next', p(10 25 50 75 90) 
local exp_10pct =`r(r1)'
local exp_25pct =`r(r2)'
local exp_50pct =`r(r3)'
local exp_75pct =`r(r4)'
local exp_90pct =`r(r5)'
restore

// Bounds computation
ereturn clear
ereturn scalar lower_bound=((`coef_outcome'-`coeff_run'*`E1' + `uncond_value'*`coeff_run')/`R1')
ereturn scalar lower_bound_neg=((`coef_outcome'-`coeff_run'*`max_unobs_term' + `uncond_value'*`coeff_run')/`R1') //special case for outcomes with negative effects
ereturn scalar upper_bound=((`coef_outcome'-`coeff_run'*`min_unobs_term' + `uncond_value'*`coeff_run')/`R1')

ereturn scalar bound_10pct = ((`coef_outcome'-`coeff_run'*`exp_10pct' + `uncond_value'*`coeff_run')/`R1')
ereturn scalar bound_25pct = ((`coef_outcome'-`coeff_run'*`exp_25pct' + `uncond_value'*`coeff_run')/`R1')
ereturn scalar bound_50pct = ((`coef_outcome'-`coeff_run'*`exp_50pct' + `uncond_value'*`coeff_run')/`R1')
ereturn scalar bound_75pct = ((`coef_outcome'-`coeff_run'*`exp_75pct' + `uncond_value'*`coeff_run')/`R1')
ereturn scalar bound_90pct = ((`coef_outcome'-`coeff_run'*`exp_90pct' + `uncond_value'*`coeff_run')/`R1')

ereturn scalar bound_manual = ((`coef_outcome'-`coeff_run'*`manual_unobs_term' + `uncond_value'*`coeff_run')/`R1')

set graphics on
end 

// Special function for the outcome: other number of candidates 

cap program drop leebounds_next_alt
program define leebounds_next_alt, eclass
syntax, outcome_next(string) running_level_next(string) band(real) [min_unobs_term(real 0)] [max_unobs_term(real 0)] [uncond_value(real 0)]
set graphics off

gen temp=`outcome_next'!=.
byso year election_type: egen temp2=max(temp)
drop if temp2==0 
drop temp*

tempname coef_outcome coeff_run R1 R1_0 R0 E1 E1_0 E0 E0_1

// Impact on outcome unconditionally 
rdrobust `outcome_next' running, h(`band') vce(nncluster code_district)
scalar `coef_outcome'=e(tau_cl)

// Impact on run + probability that a close winner runs + probability that a close winner does not run + probability that a close loser does not run
rdrobust `running_level_next' running, h(`band') vce(nncluster code_district)
scalar `coeff_run'=e(tau_cl)
scalar `R1'=e(tau_cl_r)
scalar `R1_0'=1-e(tau_cl_r)
scalar `R0'=1-e(tau_cl_l)

// Outcome value of close winner given that she runs
rdrobust `outcome_next' running if `running_level_next'==1, h(`band') vce(nncluster code_district)
scalar `E1'=e(tau_cl_r)
scalar `E0_1'=e(tau_cl_l)

// Outcome value of close winner and close loser given that she does not run
rdrobust `outcome_next' running if `running_level_next'==0, h(`band') vce(nncluster code_district)
scalar `E1_0'=e(tau_cl_r)
scalar `E0'=e(tau_cl_l)

// Bounds computation
ereturn clear
ereturn scalar upper_bound=((`coef_outcome'-`coeff_run'*`E1' -`E1_0'*`R1_0' + `E0'*`R0')/`R1')
	ereturn scalar upper_bound2=((`coef_outcome'-`coeff_run'*(`E0'-1) -`E1_0'*`R1_0' + `E0'*`R0')/`R1')
ereturn scalar lower_bound=((`coef_outcome'-`coeff_run'*`E0' -`E1_0'*`R1_0' + `E0'*`R0')/`R1')

set graphics on
end 
